//
//  WDQBViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/10.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//

#import "WDQBViewController.h"

@interface WDQBViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) MyBagHeaderView *headerView;
@end

@implementation WDQBViewController
{
    NSMutableArray *_listArray;
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的钱包";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-TabHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BackColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyBagTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    
    self.headerView = [[NSBundle mainBundle] loadNibNamed:@"MyBagHeaderView" owner:self options:nil].firstObject;
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, WIDTH, 118);
    [self obtainBagDatawithhud:YES];
    [self addRefresh];
}
- (void)addRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(obtain)];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}

#pragma mark --------- tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyBagTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    MoneyModel *model = _listArray[indexPath.row];
    cell.riqiLabel.text = [model.bring_date substringWithRange:NSMakeRange(5, 5)];
    cell.timeLabel.text = [model.bring_date substringWithRange:NSMakeRange(11, 5)];
    if ([model.type isEqualToString:@"0"]) {
        cell.moneyLabel.text = [NSString stringWithFormat:@"-%@",model.change_money];
        cell.moneyLabel.textColor = Color;
    } else {
        cell.moneyLabel.text = [NSString stringWithFormat:@"+%@",model.change_money];
        cell.moneyLabel.textColor = [UIColor redColor];
    }
    cell.detailLabel.text = model.specification;
    if ([model.status isEqualToString:@"0"]) {
        cell.stateLabel.text = @"审核中";
    } else if ([model.status isEqualToString:@"1"]) {
        cell.stateLabel.text = @"成功";
    } else {
        cell.stateLabel.text = @"失败";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoneyModel *model = _listArray[indexPath.row];
    NSString *str = model.specification;
    CGRect rect = [str boundingRectWithSize:CGSizeMake(WIDTH-69-75, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
    if (rect.size.height < 20) {
        return 55+20;
    } else {
        return 55+rect.size.height;
    }
}
- (void)obtain
{
    [self obtainBagDatawithhud:NO];
}
- (void)obtainBagDatawithhud:(BOOL)hud
{
    page = 1;
    [NetRequiest handleDataWithString:HTTPLOAD dictionary:@{@"v1":@"reviewaccount",@"uid":UID,@"page":@"1"} HUD:hud block:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        CGFloat num = [responseObject[@"result"][@"total_money"] floatValue];
        self.headerView.numLabel.text = [NSString stringWithFormat:@"%.2f",num];
        _listArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"result"][@"result"]) {
            MoneyModel *model = [[MoneyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_listArray addObject:model];
        }
        page++;
        [self.tableView reloadData];
    }];
}
- (void)loadMoreTopics
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"reviewaccount",@"uid":UID,@"page":[NSString stringWithFormat:@"%ld",page]} HUD:NO block:^(id responseObject) {
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            for (NSDictionary *dic in responseObject[@"result"][@"result"]) {
                MoneyModel *model = [[MoneyModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
